package com.example.myapplication.screen

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.Create
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material.icons.filled.Person
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material.icons.outlined.AccountCircle
import androidx.compose.material.icons.outlined.FavoriteBorder
import androidx.compose.material.icons.outlined.Home
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LargeTopAppBar
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.rememberTopAppBarState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.example.myapplication.ui.common.BottomAppBarItem
import com.example.myapplication.ui.screen.HomeScreen


@OptIn(ExperimentalMaterial3Api::class)
@Preview
@Composable
fun ScafflodDemo() {
    //控制折叠菜单展开还是收起的状态变量
    var expanded by remember { mutableStateOf(false) }
    //定义滚动行为
    val scrollBehavior =
        TopAppBarDefaults.enterAlwaysScrollBehavior(rememberTopAppBarState())
    //定义一个状态变量，表示当前选中的底部导航的索引
    var selectedIndex by remember { mutableStateOf(0) }

    //使用List存储底部导航的文本列表
    var bottomList = listOf("首页", "收藏", "我的")

    //导航控制器
    val navController = rememberNavController()

    var prePageIndex = selectedIndex

    Scaffold(
        modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
        topBar = {
            //顶部应用（标题栏）
            LargeTopAppBar(
                scrollBehavior = scrollBehavior,
                colors = TopAppBarDefaults.topAppBarColors(
                    containerColor = Color.Cyan,
                    titleContentColor = Color.Black,
                    scrolledContainerColor = Color.Cyan,
                    navigationIconContentColor = Color.Black
                ),
                title = {
                    Text("顶部标题栏")
                },
                //左侧图标
                navigationIcon = {
                    IconButton(onClick = {
                        println("返回键被点击了")
                        if (selectedIndex > 0) {
                            navController.popBackStack()    //回到上一页
                            selectedIndex = prePageIndex
                        }
                    }) {
                        Icon(
                            imageVector = Icons.AutoMirrored.Filled.ArrowBack,
                            contentDescription = "返回"
                        )
                    }
                },
                //右侧图标
                actions = {
                    IconButton(onClick = {
                        println("返回键被点击了")
                    }) {
                        Icon(imageVector = Icons.Filled.Settings, contentDescription = "设置")
                    }

                    IconButton(onClick = {
                        println("返回键被点击了")
                    }) {
                        Icon(imageVector = Icons.Filled.Create, contentDescription = "创建")
                    }

                    IconButton(onClick = {
                        expanded = true
                    }) {
                        Icon(imageVector = Icons.Filled.MoreVert, contentDescription = "更多")
                    }

                    //折叠菜单
                    DropdownMenu(
                        expanded = expanded,
                        onDismissRequest = {
                            expanded = false
                        },
                        modifier = Modifier.background(color = Color.White)
                    ) {
                        DropdownMenuItem(
                            text = { Text("选项一") },
                            onClick = {
                                println("选中了更多选项一")
                                expanded = false
                            }
                        )
                        DropdownMenuItem(
                            text = { Text("选项二") },
                            onClick = {
                                println("选中了更多选项二")
                                expanded = false
                            }
                        )
                    }

                }
            )
        },
        bottomBar = {
            //底部导航栏
            BottomAppBar(
                actions = {
                    bottomList.forEachIndexed { index, item ->
                        BottomAppBarItem(
                            normalIcon = when (index) {
                                0 -> Icons.Outlined.Home
                                1 -> Icons.Outlined.FavoriteBorder
                                2 -> Icons.Outlined.AccountCircle
                                else -> Icons.Default.Home
                            },
                            selectedIcon = when (index) {
                                0 -> Icons.Filled.Home
                                1 -> Icons.Filled.Favorite
                                2 -> Icons.Filled.Person
                                else -> Icons.Default.Home
                            },
                            text = item,
                            selected = selectedIndex == index,
                            onClick = {
                                prePageIndex = selectedIndex
                                selectedIndex = index
                                when (index) {
                                    0 -> navController.navigate("home")
                                    1 -> navController.navigate("favorite")
                                    2 -> navController.navigate("porfile")
                                }
                            },
                            modifier = Modifier.weight(1F)
                        )
                    }
                }
            )
        },
        floatingActionButton = { }
    ) { innerPadding ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(innerPadding)
        ) {
            NavHost(
                navController = navController,
                startDestination = "home" // 起始页面
            ) {
                composable("home") {
                    HomeScreen(navController)
                }
                composable("favorite") {
                    FavoritePage()
                }
                composable("porfile") {
                    ProfilePage(navController)
                }
            }
        }
    }
}




@Composable
fun HomePage() {
    val items = List(100) { "Item #$it" } // 示例数据项
    Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
        LazyColumn {
            itemsIndexed(items) { index, item ->
                Text(
                    text = item,
                    fontSize = 20.sp,
                    textAlign = TextAlign.Start,
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(50.dp)
                        .padding(start = 16.dp)
                )
            }
        }
    }
}

@Composable
fun FavoritePage() {
    Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
        Text("收藏内容", fontSize = 24.sp)
    }
}

@Composable
fun ProfilePage(navController: NavController?) {
    Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
        Text("我的内容", fontSize = 24.sp)
    }
}
